/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.upms.biz.userLog.controller;

import co.yixiang.common.core.annotation.AnonymousAccess;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.upms.biz.userLog.service.YxUserLogService;
import co.yixiang.upms.common.userLog.domain.YxUserLog;
import co.yixiang.upms.common.userLog.dto.YxUserLogDto;
import co.yixiang.upms.common.userLog.dto.YxUserLogQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
* @author guoqingming
* @date 2021-03-25
*/
@AllArgsConstructor
@Api(tags = "userOperationLog管理")
@RestController
@RequestMapping("/yxUserLog")
public class YxUserLogController {

    private final YxUserLogService yxUserLogService;
    private final IGenerator generator;


    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("hasAnyAuthority('admin','yxUserLog:list')")
    public void download(HttpServletResponse response, YxUserLogQueryCriteria criteria) throws IOException {
        yxUserLogService.download(generator.convert(yxUserLogService.queryAll(criteria), YxUserLogDto.class), response);
    }

    @AnonymousAccess
    @GetMapping
    @ApiOperation("查询userOperationLog")
    @PreAuthorize("hasAnyAuthority('admin','yxUserLog:list')")
    public ResponseEntity<PageResult<YxUserLogDto>> getYxUserLogs(YxUserLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxUserLogService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation("新增userOperationLog")
    @PreAuthorize("hasAnyAuthority('admin','yxUserLog:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxUserLog resources){
        return new ResponseEntity<>(yxUserLogService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改userOperationLog")
    @PreAuthorize("hasAnyAuthority('admin','yxUserLog:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxUserLog resources){
        yxUserLogService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除userOperationLog")
    @PreAuthorize("hasAnyAuthority('admin','yxUserLog:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxUserLogService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
